package zstu.edu.cn.notepad;

import android.support.annotation.NonNull;
import android.util.Log;


import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Post {
    private static Request request;
    public static JSONObject res;

    public static void requestPost(String Url, RequestBody requestBody, final TtitCallback callback) {
        try {
            request = new Request.Builder()
                    .url("http://" + Common.IP + ":" + Url)
                    .post(requestBody)
                    .build();
            OkHttpClient client = new OkHttpClient();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    Log.d("TAG", "onFailure: ===>");
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    try {
                        callback.onSuccess(response.body().string());
                        Log.d("TAG", "onResponse: ======>" + response.body().string());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
